<template>
	<view>
		<u-sticky>
			<view class="header"><u-search placeholder="请输入关键字" v-model="keyword" shape="square" :showAction="false" @search="searchlist"></u-search></view>
			<view class="tags"><u-tabs :list="list1" @click="click" width="300rpx"></u-tabs></view>
		</u-sticky>
		<view class="content">
			<view class="box" v-for="item in orderList">
				<view class="" @tap="goDetail(item)">
					<view class="title">
						<view class="title-con" v-if="item.orderStatus == 0">待付款</view>
						<view class="title-con" v-else-if="item.orderStatus == 1">待接单</view>
						<view class="title-con" v-else-if="item.orderStatus == 2">待发货</view>
						<view class="title-con" v-else-if="item.orderStatus == 3">待收货</view>
						<view class="title-con" v-else-if="item.orderStatus == 4">待使用</view>
						<view class="title-con" v-else-if="item.orderStatus == 5" style="color: #4cd909">已完成</view>
						<view class="title-con" v-else-if="item.orderStatus == 6" style="color: #606266">已取消</view>
						<view class="title-con" v-else-if="item.orderStatus == 7" style="color: #606266">已失效</view>
						<view class="title-con" v-else-if="item.orderStatus == 8">退款中</view>
						<view class="title-con" v-else-if="item.orderStatus == 9">已退款</view>
						<view class="title-con" v-else-if="item.orderStatus == 10">未退款</view>
					</view>
					<view class="box-con">
						<view class="box-con-left">商品合计</view>
						<view class="box-con-right">
							￥{{ item.totalMoney }}
							<!-- <text>(共一件商品)</text> -->
						</view>
					</view>
					<view class="box-con">
						<view class="box-con-left">实际支付</view>
						<view class="box-con-right">
							￥{{ item.payMoney }}
							<!-- <text>(含运费)</text> -->
						</view>
					</view>
					<view class="box-con">
						<view class="box-con-left">配送方式</view>
						<view class="box-con-right" v-if="item.logisticsType == 0">快递</view>
						<view class="box-con-right" v-else-if="item.logisticsType == 1">配送</view>
						<view class="box-con-right" v-else-if="item.logisticsType == 2">快递</view>
						<view class="box-con-right" v-else-if="item.logisticsType == 3">自提</view>
						<view class="box-con-right" v-else-if="item.logisticsType == 4">到店消费</view>
						<view class="box-con-right" v-else-if="item.logisticsType == 5">上门服务</view>
						<view class="box-con-right" v-else-if="item.logisticsType == 7">货到付款</view>
					</view>
					<view class="box-con">
						<view class="box-con-left">下单时间</view>
						<view class="box-con-right">{{ item.createTime }}</view>
					</view>
					<view class="box-con">
						<view class="box-con-left">订单编号</view>
						<view class="box-con-right">{{ item.orderNo }}</view>
					</view>
					<view class="box-con" v-if="item.orderRemark">
						<view class="box-con-left">订单备注</view>
						<view class="box-con-right">{{ item.orderRemark }}</view>
					</view>
					
				</view>

				<view class="footer">
					<view class="butt" v-if="item.orderStatus == 2&&item.logisticsType==2" @tap="send(item.id,2)" style="background-color: orange;color: #fff;border: none;">快递发货</view>
					<view class="butt" v-if="item.orderStatus == 2&&item.logisticsType==1" @tap="send(item.id,1)" style="background-color: orange;color: #fff;border: none;">配送</view>
					<view class="butt" v-if="item.orderStatus == 2&&item.logisticsType==3" @tap="send(item.id,3)" style="background-color: orange;color: #fff;border: none;">核销</view>
					<view class="butt" v-if="item.orderStatus == 6 || item.orderStatus == 7" @tap="del(item)">删除订单</view>
					<!-- <view class="butt">补打小票</view> -->
				</view>
				
			</view>
			<view class="none_list_box" style="margin-top: 200rpx;" v-if="orderList.length===0">
				<u-empty mode="data" ></u-empty>
			</view>
		</view>
		
		<u-popup :show="show" @close="close" closeable mode="center"  round="20">
			<view style="padding:20rpx;width: 600rpx;">
				<view class="" style="text-align: center;font-weight: 700;text-align: center;padding-bottom: 20rpx;border-bottom: 1rpx solid #f5f5f5;">{{titleName}}</view>
				<view class="" style="font-size: 26rpx;padding: 20rpx 0;">
					<view class="" style="display: flex;align-items: center;margin-bottom: 20rpx;">
						<view class="" style="width: 110rpx;">
							方式
						</view>
						<view class="" style="margin-left: 30rpx;" v-if="info.logisticsType==1">配送</view>
						<view class="" style="margin-left: 30rpx;" v-if="info.logisticsType==2">快递</view>
						<view class="" style="margin-left: 30rpx;" v-if="info.logisticsType==3">自提</view>
					</view>
					<view class="" style="display: flex;align-items: center;margin-bottom: 20rpx;">
						<view class="" style="width: 110rpx;">
							订单编号
						</view>
						<view class="" style="margin-left: 30rpx;">{{info.orderLogistics.orderNo}}</view>
					</view>
					<view class="" v-if="info.logisticsType==2">
						<view class="" style="display: flex;align-items: center;margin: 20rpx 0;" @click="showSex = true">
							物流公司
							<view class="" style="margin-left: 30rpx;width: 300rpx;">
								<u--input
									placeholder="请输入物流公司名称"
									border="surround"
									v-model="info.orderLogistics.expressName"
									disabledColor="#ffffff"
									prefixIconStyle="font-size: 16px;color: #909399"
								></u--input>
							</view>
						</view>
						<view class="" style="display: flex;align-items: center;">
							物流单号
							<view class="" style="margin-left: 30rpx;width: 300rpx;"><u--input placeholder="请输入物流单号" border="surround" v-model="info.orderLogistics.expressNo"></u--input></view>
							<view class=""  @tap="scanCode"><u-icon name="scan" color="#2979ff" size="28"></u-icon></view>
						</view>
					</view>
					<view class="" v-if="info.logisticsType==1">
						
						<view class="" style="display: flex;align-items: center;">
							骑手手机号
							<view class="" style="margin-left: 30rpx;width: 300rpx;"><u--input placeholder="请输入骑手手机号" border="surround" v-model="info.orderLogistics.riderPhone"></u--input></view>
						</view>
					</view>
				</view>
				<view class="" style="display: flex;align-items: center;justify-content: center;" @click="subfa">
					<view
						class=""
						style="display: flex;justify-content: center;align-items: center;width: 350rpx;height: 60rpx;color: #fff;background-color: orange;border-radius: 30rpx;"
					>
						确定
					</view>
				</view>
			</view>
		</u-popup>
		
	</view>
</template>

<script>
import { searchOrderList, getOrderInfoById, delOrder, verificationOrder,
orderSendGoods,updateOrderLogistics,synchronousWeChartOrderLogisticsStatus,followWaybill } from '../../../utils/api.js';
export default {
	data() {
		return {
			show: false,
			showSex: false,
			value: '顺丰快递',
			actions: [
				{
					name: '顺丰快递'
				},
				{
					name: '圆通快递'
				},
				{
					name: '申通快递'
				},
				{
					name: '韵达快递'
				},
				{
					name: '极兔快递'
				}
			],
			list1: [
				{ id: -1, name: '全部订单' },
				{ id: 0, name: '待付款' },
				{ id: 1, name: '待接单' },
				{ id: 2, name: '待发货/待服务/待取货' },
				{ id: 3, name: '待收货/待确认' },
				{ id: 4, name: '待使用' },
				{ id: 5, name: '已完成' },
				{ id: 6, name: '已取消' },
				{ id: 7, name: '已失效' },
				{ id: 8, name: '退款中' },
				{ id: 9, name: '退款成功' },
				{ id: 10, name: '退款失败' }
			],
			orderList: [],
			search: {
				page: 1,
				size: 10
			},
			keyword: '',
			businessId: -1,
			orderStatus: -1,
			total: 0,
			info: {},
			titleType:2,
			titleName:'快递发货',
			zitiList:[]
		};
	},
	onShow() {
		this.getOrderlist();
	},
	onReachBottom() {
		if (this.orderList.length < this.total) {
			this.search.page++;
			this.getOrderlist();
		}
	},
	methods: {
		sexSelect(e) {
			this.info.orderLogistics.expressName = e.name;
		},
		searchlist() {
			this.getOrderlist();
			this.keyword = '';
		},
		close() {
			this.show = false;
		},
		click(item) {
			console.log('item', item);
			this.orderStatus = item.id;
			this.orderList = [];
			this.search.page = 1;
			this.getOrderlist();
		},
		goDetail(item) {
			uni.navigateTo({
				url: '/pages/order/orderDetail/orderDetail?id=' + item.id
			});
		},
		getOrderlist() {
			let data = {
				page: this.search.page,
				size: this.search.size,
				keyword: this.keyword,
				adminId: uni.getStorageSync('userInfo').id,
				userServiceToken: uni.getStorageSync('userInfo').userServiceToken,
				businessId: this.businessId,
				orderStatus: this.orderStatus
			};
			uni.showLoading({
				title: '加载中...',
				mask: true
			});
			
			searchOrderList(data).then(res => {
				if (res.code == 200) {
					if (this.search.page == 1) {
						this.orderList = [];
					}
					uni.hideLoading();
					let arr = [];
					arr = this.orderList.concat(res.body.list);
					this.orderList = arr;
					this.total = res.body.total;
					// businessId.value = "";
					// tableobj.keyword = "";
				}
			});
		},
		del(item) {
			console.log(item);
			let data = {
				ids: item.id
			};
			delOrder(data).then(res => {
				if (res.code == 200) {
					uni.showToast({
						icon: 'none',
						title: '删除成功'
					});
					setTimeout(() => {
						this.getOrderlist();
					}, 500);
				}
			});
		},
		send(id,num) {
			console.log(id);
			uni.showLoading({
				title: '获取订单信息中...',
				mask: true
			});
			this.titleType=num
			if(num==3){
				this.titleName='订单核销'
			}else if(num==2){
				this.titleName='快递配送'
			}else if(num==1){
				this.titleName='骑手配送'
			}
			
			 let data = {
			 	adminId: uni.getStorageSync('userInfo').id,
			 	userServiceToken: uni.getStorageSync('userInfo').userServiceToken,
			 	id: id.toString()
			 };
			 getOrderInfoById(data).then(res => {
			 	if (res.code == 200) {
					uni.hideLoading();
					this.show = true;
			 		this.info = res.body;
					this.zitiList=res.body.orderItemList
					
			 	}
			 });
		},
		scanCode() {
						// 允许从相机和相册扫码
						uni.scanCode({
							scanType: ["qrCode"],
							success: (res) => {
								console.log(res);
								if (res.result) {
									this.info.orderLogistics.expressNo = res.result;
								} else {
									console.log('请重新扫描');
									return false;
								}
							},
							fail: (res) => {
								console.log('未识别到');
							}
						})
					},
		subfa() {
			console.log(this.info);
			
			if(this.titleType==1){
				uni.showLoading({
					title: '正在发货中...',
					mask: true
				});
				updateOrderLogistics(this.info.orderLogistics).then(res=>{
					if(res.code==200){
						uni.hideLoading();
						let data={
							orderIds:this.info.id.toString()
						}
						orderSendGoods(data).then(res => {
							if (res.code == 200) {
								this.show = false;
								this.getOrderlist();
								// this.orderId()
								this.WeChartOrder()
							}
						});
					}
				})
			}else if(this.titleType==2){
				uni.showLoading({
					title: '正在发货中...',
					mask: true
				});
				updateOrderLogistics(this.info.orderLogistics).then(res=>{
					if(res.code==200){
						let data={
							orderIds:this.info.id.toString()
						}
						orderSendGoods(data).then(res => {
							if (res.code == 200) {
								uni.hideLoading();
								this.show = false;
								this.getOrderlist();
								this.orderId()
								this.WeChartOrder()
							}
						});
					}
				})
			}else if(this.titleType==3){
				uni.showLoading({
					title: '正在核销中...',
					mask: true
				});
				let list =[]
				this.zitiList.forEach(item=>{
					list.push(item.verificationCode)
				})
				let data={
					verificationCodeStr: list.toString(),
				}
				verificationOrder(data).then(res=>{
					if(res.code==200){
						
						uni.hideLoading();
						this.show = false;
						this.getOrderlist();
						this.WeChartOrder()
					}
				})
			}
			
		},
		
		// 同步微信物流信息
		orderId (){
		  let data = {
		    orderId: this.info.orderLogistics.orderId,
		    LogisticNo:this.info.orderLogistics.expressNo,
		  }
		  followWaybill( data).then((res) => {
		    if (res.code == 200) {
		    }
		  })
		},
		// 与微信官方同步商品发货
		WeChartOrder (){
		  let data = {
		    orderId: this.info.orderLogistics.orderId,
		  }
		  synchronousWeChartOrderLogisticsStatus( data).then((res) => {
		    if (res.code == 200) {
				uni.showToast({
					title: '信息同步成功',
					duration: 2000
				});
		    }
		  })
		},
		// 订单核销
		hexiaoOrder(){
			let data={
				verificationCodeStr:''
			}
			verificationOrder(data)
		}
		
		

		
	}
};
</script>

<style lang="scss">
page {
	background-color: #f5f5f5;
}
</style>
<style lang="scss" scoped>
.header {
	display: flex;
	align-items: center;
	padding: 20rpx;
	background-color: #fff;
	.button {
		width: 180rpx;
		height: 60rpx;
		.btn {
			color: #fff;
			height: 60rpx;
			line-height: 60rpx;
			font-size: 26rpx;
			background-color: #6da7ff;
			text-align: center;
		}
	}
}
.tags {
	background-color: #fff;
	// /deep/.u-tabs__wrapper__nav__item{
	// 	padding: 0 80rpx;
	// }
}
.content {
	padding: 20rpx;
	.box {
		background-color: #fff;
		border-radius: 20rpx;
		margin-bottom: 20rpx;
		position: relative;
		.title {
			padding: 40rpx;
			border-bottom: 1rpx solid #f5f5f5;

			.title-con {
				width: 100rpx;
				height: 100rpx;
				border-radius: 50rpx;
				border: 1rpx solid #999;
				font-size: 26rpx;
				display: flex;
				background-color: #fff;
				align-items: center;
				justify-content: center;
				color: #999;
				font-weight: 700;
				transform: rotate(-30deg);
				position: absolute;
				right: 20rpx;
				top: 30rpx;
			}
		}
		.box-con {
			display: flex;

			align-items: center;
			padding: 20rpx 20rpx 0;
			font-size: 26rpx;
			.box-con-left {
				color: #999;
				margin-right: 30rpx;
			}
			.box-con-right {
				color: #1d1e23;
			}
		}

		.footer {
			padding: 20rpx;
			display: flex;
			align-items: center;
			justify-content: flex-end;
			border-top: 1rpx solid #f5f5f5;
			margin-top: 20rpx;
			.butt {
				width: 180rpx;
				height: 50rpx;
				font-size: 26rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				color: #999;
				border: 1rpx solid #999;
				border-radius: 25rpx;
				margin: 0 0 0 30rpx;
			}
		}
	}
}
</style>
